package cn.car91.adapter;

import java.util.ArrayList;
import java.util.List;

import com.lidroid.xutils.util.LogUtils;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import cn.car91.R;
import cn.car91.config.InitApplication;
import cn.car91.utils.RegexUtil;

public class SearchItemsAdapter extends ArrayAdapter {
    int resource;
    public static class SearchItemInfo{
        private String id;
        private String name;
        private String other;
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getOther() {
            return other;
        }
        public void setOther(String other) {
            this.other = other;
        }
    }
    List<SearchItemInfo> searchItems;

    public List<SearchItemInfo> getSearchItems() {
        return searchItems;
    }
    public void setSearchItems(List<SearchItemInfo> searchItems) {
        this.searchItems = searchItems;
    }

    Context context;
    InitApplication config;

    public SearchItemsAdapter(Context context, int resource) {
        super(context, resource);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.resource = resource;
        config = (InitApplication) context.getApplicationContext();
        searchItems = new ArrayList<SearchItemInfo>();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return searchItems.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return searchItems.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }
    
    public String getSearchItemId(int arg0) {
        // TODO Auto-generated method stub
        return searchItems.get(arg0).id;
    }

    private class SearchItem{
        public TextView name;
        public TextView other;
    }
    
    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        SearchItem searchItem = null;
        //if (arg1 == null) {
            arg1 = layoutInflater.inflate(resource, null);
            searchItem=new SearchItem();
            searchItem.name = (TextView) arg1.findViewById(R.id.search_name);
            searchItem.other = (TextView) arg1.findViewById(R.id.search_other);

            /*arg2.setTag(searchItem);
        } else {
            searchItem = (SearchItem) arg2.getTag();
        }*/
        
        try {
            searchItem.name.setText(Html.fromHtml(searchItems.get(arg0).name));
            if(RegexUtil.isNotNull(searchItems.get(arg0).other)){
                searchItem.other.setText(searchItems.get(arg0).other);
                searchItem.other.setVisibility(View.VISIBLE);
            } else {
                searchItem.other.setVisibility(View.GONE);
            }
        } catch (Exception e) { }

        return arg1;
    }
}
